import {ActionContext} from 'vuex';
import ListModule from '@/store/modules/list-module';
import ListState from '@/store/modules/list-state';
import FlowInstance from '@/store/entities/flow/flow-instance/flow-instance';
import Ajax from '@/lib/ajax';
import PageResult from '@/store/entities/page-result';

interface FlowInstanceState extends ListState<FlowInstance> {
    instanceForVerify: any,
    instanceForEdit:any,
    operationHistories:any[],
    excelFile:any,
    backlogCount:number,
}

class FlowInstanceModule extends ListModule<FlowInstanceState, any, FlowInstance> {
    state = {
        totalCount: 0,
        currentPage: 1,
        pageSize: 10,
        list: new Array<FlowInstance>(),
        loading: false,
        instanceForVerify:{},
        instanceForEdit:{},
        operationHistories:[],
        excelFile:{},
        backlogCount:0,
    };
    actions = {
        async obtainFlowInstances(context:ActionContext<FlowInstanceState, any>,payload:any){
            context.state.loading=true;
            const response=await Ajax.post('/api/services/app/FlowInstance/ObtainFlowInstances',payload.data)
            context.state.loading=false;
            const instance=response.data.result as PageResult<FlowInstance>;
            context.state.totalCount=instance.totalCount;
            context.state.list=instance.items;
        },
        async createOrUpdateFlowInstance(context:ActionContext<FlowInstanceState, any>,payload:any){
            await Ajax.post('/api/services/app/FlowInstance/CreateOrUpdateFlowInstance',payload.data);
        },
        async workingFlowInstance(context:ActionContext<FlowInstanceState, any>,payload:any){
            await Ajax.post('/api/services/app/FlowInstance/WorkingFlowInstance',payload.data);
        },
        async restartFlowInstance(context:ActionContext<FlowInstanceState, any>,payload:any){
            await Ajax.post('/api/services/app/FlowInstance/RestartFlowInstance',payload.data);
        },
        async recallFlowInstance(context:ActionContext<FlowInstanceState, any>,payload:any){
            await Ajax.post('/api/services/app/FlowInstance/RecallFlowInstance',payload.data);
        },
        async getFlowInstanceForVerify(context:ActionContext<FlowInstanceState, any>,payload:any){
            const response=await Ajax.get('/api/services/app/FlowInstance/GetFlowInstanceForVerify',{params:payload.data});
            context.state.instanceForVerify=response.data.result.flowInstance;
        },
        async getFlowInstanceForEdit(context:ActionContext<FlowInstanceState, any>,payload:any){
            const response=await Ajax.get('/api/services/app/FlowInstance/GetFlowInstanceForEdit',{params:payload.data});
            context.state.instanceForEdit=response.data.result.flowInstance;
        },
        async verifyNode(context:ActionContext<FlowInstanceState, any>,payload:any){
            await Ajax.post('/api/services/app/FlowInstance/VerifyNode',payload.data);
        },
        async getFlowOperationHistories(context:ActionContext<FlowInstanceState, any>,payload:any){
            const response=await Ajax.get('/api/services/app/FlowInstance/GetFlowOperationHistories',{params:payload.data});
            context.state.operationHistories=response.data.result.items;
        },
        async deleteFlowInstance(context:ActionContext<FlowInstanceState, any>,payload:any){
            await Ajax.delete('/api/services/app/FlowInstance/DeleteFlowInstance?Id=' + payload.data.id);
        },
        async obtainFlowInstancesToExcel(context:ActionContext<FlowInstanceState, any>,payload:any){
            const response=await Ajax.post('/api/services/app/FlowInstance/ObtainFlowInstancesToExcel',payload.data);
            context.state.excelFile=response.data.result;
        },
        async getBacklogCount(context:ActionContext<FlowInstanceState, any>,payload:any){
            const response=await Ajax.get('api/services/app/FlowInstance/GetBacklogCount');
            context.state.backlogCount=response.data.result;
        }

    };
    mutations = {
        setCurrentPage(state: FlowInstanceState, page: number) {
            state.currentPage = page;
        },
        setPageSize(state: FlowInstanceState, pageSize: number) {
            state.pageSize = pageSize;
        },
    };
}

const flowInstanceModule = new FlowInstanceModule();
export default flowInstanceModule;
